ngx.req.read_body()
local raw = ngx.req.get_body_data()
local json = require "cjson"
local args
pcall(function(raw) args = json.decode(raw) end, raw)

if nil == args then
    ngx.err("no post data")
    return
end

local id = args.id

if nil == id then
    ngx.err('id不能为空')
    return
end

local uid = args.uid

if nil == uid then
    ngx.err("uid不能为空")
    return
end

local name = args.name

if nil == name then
    ngx.err("姓名不能为空")
    return
end

local mobile = args.mobile

if nil == mobile then
    ngx.err("手机号码不能为空")
    return
end

local activity_id = args.act_id

if nil == activity_id then
    ngx.err("act_id不能为空")
    return
end

local prize_id = args.prize_id

if nil == prize_id then
    ngx.err("prize_id不能为空")
    return
end

local coupon = args.coupon
local redis_client = require "lib.redis"
local redis = redis_client:new()

local record = {
    id=id,
    open_id=uid or args.open_id,
    activity_id=activity_id,
    prize_id=prize_id,
    name=name,
    mobile=mobile,
    address=args.address or "",
    coupon=coupon or "",
    times=1
}

local _, err = redis:lpush("exchange:records", json.encode(record))

if nil == err then
    ngx.say([[{"code":200,"message":"success"}]])
else
    ngx.err("redis error:"..err)
end